package com.shaozi.nc.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shaozi.data.mq.business.base.MqConsts;
import com.shaozi.data.mq.business.base.SourceTypeConsts;
import com.shaozi.data.mq.business.bean.MQExchangeData;
import com.shaozi.data.mq.business.bean.MQExchangeRuleBean;
import com.shaozi.data.mq.business.bean.MQSyncItem;
import com.shaozi.lib.core.ActionRetBean;
import com.shaozi.lib.core.StatusCode;
import com.shaozi.nc.mq.producer.SendIMInfoProducer;
import com.shaozi.nc.web.action.base.BaseActionImpl;
import com.shaozi.nc.web.action.bean.ThirdPartyMessageBean;
import com.shaozi.utils.common.BeanUtils;

@Controller
@RequestMapping(value = "/thirdParty")
public class ThirdPartyAction extends BaseActionImpl {
	private final Logger logger = Logger.getLogger(ThirdPartyAction.class);

	public ThirdPartyAction() {

	}

	@ResponseBody
	@RequestMapping(value = "/sendMessage", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
	public ActionRetBean sendMessage(HttpServletRequest req, HttpServletResponse res,
			@RequestBody ThirdPartyMessageBean tpMsg) {
		ActionRetBean ret = new ActionRetBean();
		if (StringUtils.isEmpty(tpMsg.getCompId()) || StringUtils.isEmpty(tpMsg.getFrom())
				|| StringUtils.isEmpty(tpMsg.getTo()) || StringUtils.isEmpty(tpMsg.getMsgType())
				|| StringUtils.isEmpty(tpMsg.getContent())) {
			ret.setCode(StatusCode.CODE_Critical_Param_lost);
		} else {
			MQExchangeData<MQExchangeRuleBean<ThirdPartyMessageBean>> data = new MQExchangeData<MQExchangeRuleBean<ThirdPartyMessageBean>>();
			MQExchangeRuleBean<ThirdPartyMessageBean> rule = new MQExchangeRuleBean<ThirdPartyMessageBean>();
			MQSyncItem sync = new MQSyncItem();

			//
			sync.setSourceType(SourceTypeConsts.Sync.ThirdPartyInfoSend.ThirdPartyInfo_Send);
			rule.setSync(sync);

			rule.setBiz(tpMsg);

			data.setAction_type(MqConsts.ActionType.Sync);
			data.setRule(rule);
			data.setCompanyId(tpMsg.getCompId());

			SendIMInfoProducer producer = (SendIMInfoProducer) BeanUtils.getBean(SendIMInfoProducer.class);
			producer.push(data);
			ret.setCode(StatusCode.CODE_OK);
		}
		return ret;
	}
}
